/*----------------------------------------------------*
       Project : Covid 19
       Purpose : Main Paper Regression and Table for ASHA Survey
       updated : September 24
*-----------------------------------------------------*/

*=======================================================================*

** SETTING UP
version 15
clear all
pause on
set more off
qui cap log c
set scheme plotplain


loc path_LM = "/Users/louis-maeljean/Dropbox (MIT)/West Bengal Information Campaign/AER_I/for_submission"
loc path = "`path_LM'" 		//other users should change this

cd "`path'"



*========================================================================*

/*PLEASE READ:

In this file Table 1 and Figure 1 are implemented (ASHA survey) 

Throughout the file we use the following variables:
	fever				: number of fever cases reported to the ASHA in the last 3 days
	respiratory_count 	: number of respiratory cases reported to the ASHA in the last 3 days
	total_symptoms_fr 	: sum of fever and respiratory
	any_symptoms_fr		: binary for whether either fever or respiratory is positive
	Wflw_households		: number of households under the ASHA's supervison
	

And the following fixed effects
	phase 		  		: survey phase (ranging from 0 to 5 where 1 is the window for the main analysis -07-10 may-)
	interview_round		: current ASHA's interview round (e.g. 3 if ASHA is being interviewed for the third time)
	smartphone_yn		: 0 if ASHA has no access to a smartphone, 1 if has own smartphone, 2 if family member has a smartphone
	district			: district the ASHA lives in
	id_date				: date of interview
	
*/


** LOADING PROCESSED DATASET **


use "`path'/data/outcomes_asha_reg_input3", clear   // look at 01_processing for how this was generated
cd "`path'/output"

*========================================================================================================================
     *Table 1 | Reports Received by ASHAs Within 5 Days of Messaging*
*========================================================================================================================

preserve

keep if phase == 1  //Only keep data for the 07-10 May window
postfile q_values2 p_val using Q_values_tab1, replace //create .dta file named Q_values_tab1 where p_values will be stored (p_val) to perform multiple hypothesis testing on treatment coefficients


//Fever
reg fever Treatment Wflw_households i.id_date i.district i.interview_round i.smartphone, vce(cluster pincode)
estimates store fever_MHT
su fever if e(sample) & Treatment == 0 //get control mean
loc fever_mean = r(mean) 
quietly test Treatment
post q_values2 (r(p)) //get p_value for Treatment coefficient and post it in q_values2 file

//Respiratory
reg respiratory_count Treatment Wflw_households i.id_date i.district i.interview_round i.smartphone, vce(cluster pincode)
estimates store respiratory_MHT
su respiratory_count if e(sample) & Treatment == 0
loc resp_mean = r(mean) 
quietly test Treatment
post q_values2 (r(p))

//Any symptoms
reg any_symptoms_fr Wflw_households Treatment i.id_date i.district i.smartphone i.interview_round, vce(cluster pincode)
estimates store anySymptoms_MHT
su any_symptoms_fr if e(sample) & Treatment == 0
loc anyS_mean = r(mean)
quietly test Treatment
post q_values2 (r(p))

//Total symptoms
reg total_symptoms_fr  Wflw_households Treatment  i.id_date i.district i.interview_round i.smartphone, vce(cluster pincode)
estimates store totalSymptoms_MHT
su total_symptoms_fr if e(sample) & Treatment == 0
loc totalS_mean = r(mean)
quietly test Treatment
post q_values2 (r(p))

postclose q_values2

frame create temp 				//create temporary frame to store q_values 
frame change temp
	use Q_values_tab1, clear 	//open Q_values_tab1 to compute q_values
	qqvalue p_val, method(hochberg) qvalue(q) //compute q_values using hochberg correction
	loc fever_qval = q[1]
	loc resp_qval = q[2]
	loc any_qval = q[3]
	loc total_qval = q[4]
	
frame change default 		//back to default frame
	

outreg2 [fever_MHT] using "Tables/Table1", ///
keep(Treatment) replace ///
label tex(frag) stats(coef se pval) noaster nonotes nor2 nocons par(se) bracket(pval) dec(3) adec(3) ///
addstat("Control Mean", `fever_mean', "q_value", `fever_qval') ///
addtext("District FE", \checkmark, "Total rounds FE", \checkmark, "Smartphone FE", \checkmark, "Date FE", \checkmark) ///
ctitle(Number of Fever Cases)

outreg2 [respiratory_MHT] using "Tables/Table1", ///
keep(Treatment) append ///
label tex(frag) stats(coef se pval) noaster nonotes nor2 nocons par(se) bracket(pval) dec(3) adec(3) ///
addstat("Control Mean", `resp_mean', "q_value", `resp_qval') ///
addtext("District FE", \checkmark, "Total rounds FE", \checkmark, "Smartphone FE", \checkmark, "Date FE", \checkmark) ///
ctitle(Number of Respiratory Cases)

outreg2 [anySymptoms_MHT] using "Tables/Table1", ///
keep(Treatment) append ///
label tex(frag) stats(coef se pval) noaster nonotes nor2 nocons par(se) bracket(pval) dec(3) adec(3) ///
addstat("Control Mean", `anyS_mean', "q_value", `any_qval') ///
addtext("District FE", \checkmark, "Total rounds FE", \checkmark, "Smartphone FE", \checkmark, "Date FE", \checkmark) ///
ctitle(Any Symptoms Reported)

outreg2 [totalSymptoms_MHT] using "Tables/Table1", ///
keep(Treatment) append ///
label tex(frag) stats(coef se pval) noaster nonotes nor2 nocons par(se) bracket(pval) dec(3) adec(3) ///
addstat("Control Mean", `totalS_mean', "q_value", `total_qval') ///
addtext("District FE", \checkmark, "Total rounds FE", \checkmark, "Smartphone FE", \checkmark, "Date FE", \checkmark) ///
ctitle(Total Number of Cases)

frame drop temp //delete temp frame
	
restore

erase "Q_values_tab1.dta" 


*=================================================================
                    *Figure 1*
*=================================================================

//Fever
reghdfe fever c.Wflw_households##phase Treatment#phase if enter_phase < 2 & phase > 0, absorb(id_date phase#district phase#smartphone_yn phase#interview_round) cluster(pincode)
estimates store Fever

//Respiratory
reghdfe respiratory_count c.Wflw_households##phase Treatment#phase if enter_phase < 2 & phase > 0, absorb(id_date phase#district phase#smartphone_yn phase#interview_round) cluster(pincode)
estimates store Respiratory


//Get the p-values for the one-sided test that Resp - Fever < 0 against the null that Resp - Fever >= 0
quietly reg fever c.Wflw_households##phase Treatment#phase i.id_date i.phase#district i.phase#smartphone_yn i.phase#interview_round if enter_phase < 2 & phase > 0
	estimates store fever
quietly reg respiratory_count c.Wflw_households##phase Treatment#phase i.id_date i.phase#district i.phase#smartphone_yn i.phase#interview_round if enter_phase < 2 & phase > 0
	estimates store respiratory

quietly suest fever respiratory, cluster(pincode)

test _b[respiratory_mean:1.Treatment#1.phase] - _b[fever_mean:1.Treatment#1.phase] = 0
	loc p1 = round(normal(-1*sqrt(r(chi2))),0.001)				//Computing one-tailed p_values from two sided test
	loc p1 : di %9.3f `p1'
quietly test _b[respiratory_mean:1.Treatment#2.phase] - _b[fever_mean:1.Treatment#2.phase] = 0
	loc p2 = round(normal(-1*sqrt(r(chi2))),0.001)
	loc p2 : di %9.3f `p2'
quietly test _b[respiratory_mean:1.Treatment#3.phase] - _b[fever_mean:1.Treatment#3.phase] = 0
	loc p3 = round(normal(-1*sqrt(r(chi2))),0.001)
	loc p3 : di %9.3f `p3'
 test _b[respiratory_mean:1.Treatment#4.phase] - _b[fever_mean:1.Treatment#4.phase] = 0
	loc p4 = round(normal(-1*sqrt(r(chi2))),0.001)
	loc p4 : di %9.3f `p4'
quietly test _b[respiratory_mean:1.Treatment#5.phase] - _b[fever_mean:1.Treatment#5.phase] = 0
	loc p5 = round(normal(sqrt(r(chi2))),0.001)
	loc p5 : di %9.3f `p5'

	 
//Processing the plot
coefplot (Fever, baselevels label(Number of Fever Cases) text(1 0.80 "p_diff = `p1'" 2 0.80 "p_diff = `p2'" 3 0.80 "p_diff = `p3'" 4.1 0.80 "p_diff = `p4'" 5 0.80 "p_diff = `p5'",size(vsmall)) text(0.5 0.80 "H0: resp - fever >=0", size(tiny) color("navy"))) (Respiratory, baselevels label(Number of Respiratory Cases)), keep(1.Treatment*) xline(0) coeflabels(1.Treatment#1.phase = "Phase 1 Treatment Effect (07-10 May) " 1.Treatment#2.phase = "Phase 2 Treatment Effect (11-19 May)" ///
			 1.Treatment#3.phase = "Phase 3 Treatment Effect (20 May-05 Jul)" 1.Treatment#4.phase = "Phase 4 Treatment Effect (01-31 Aug)" ///
			 1.Treatment#5.phase = "Phase 5 Treatment Effect (01-15 Sep)", labsize(small)) mlabel("{it:p} = " + string(@pval,"%9.3f")) mlabsize(vsmall) mlabposition(4) mlabgap(*2) levels(95 90) legend(order(1 "95" 2 "90" 3 "Fever" 6 "Respiratory") position(6) cols(4)) xlabel(,format(%03.1f))
			 
graph export "Figures/Figure1.png", replace				 
			 
preserve

keep flw_fever flw_respiratory_count Wflw_households phase Treatment id_date district smartphone_yn interview_round pincode unique_id
*export excel using "Figures/Figure1_code", replace

restore

******************************
*** END **********************
******************************


